package com.xilihui.xlh.business.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.LoginActivity;
import com.xilihui.xlh.business.activitys.TakeGoodsActivity;
import com.xilihui.xlh.business.activitys.store.CartActivity;
import com.xilihui.xlh.business.activitys.store.StoreSearchActivity;
import com.xilihui.xlh.business.activitys.store.TradingCenterActivity;
import com.xilihui.xlh.business.adapters.BasePagerAdapter;
import com.xilihui.xlh.business.entities.PayEntity;
import com.xilihui.xlh.business.entities.StoreCartListEntity;
import com.xilihui.xlh.business.entities.StoreHomeDataEntity;
import com.xilihui.xlh.business.entities.TakeGoodsEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/16.
 */

public class StoreFragment extends BaseCompatFragment implements View.OnClickListener {

    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_goodNum)
    TextView tv_goodNum;
    
    ExceptionManager exceptionManager;
    
    boolean isLogin = false;
    
    int page = 1;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_store,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        YEventBuses.register(this);
//        exceptionManager = new ExceptionManager(viewPager,new DefaultExceptionListener(this)){
//            @Override
//            protected int getRetryView() {
//                return R.layout.base_empty;
//            }
//        };
//        
//        exceptionManager.hide();
        isLogin = (boolean) SPUtil.get(getActivity(),SPUtil.IS_LOGIN,false); 
        getData();
        if(isLogin){
            getJoinGoods();
        }
    }
    
    public void getData(){
        StoreRequest.home(page)
                .compose(DoTransform.<StoreHomeDataEntity>applyScheduler(getActivity(),true))
                .compose(this.<StoreHomeDataEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreHomeDataEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(final StoreHomeDataEntity entity) {
                        
                        ArrayList<StoreHomeDataEntity.GoodsCategoriesBean> goods = 
                                (ArrayList<StoreHomeDataEntity.GoodsCategoriesBean>) entity.getGoods_categories();
                        ArrayList<String> titles = new ArrayList<>();
                        
                        if(goods!=null&&goods.size()>0){
//                            exceptionManager.hide();
                            for (int i=0;i<goods.size();i++){
                                titles.add(goods.get(i).getMobile_name());
                            }
                            
                            BasePagerAdapter basePagerAdapter = new BasePagerAdapter(getActivity().getSupportFragmentManager(),
                                    getActivity(),titles);

                            basePagerAdapter.setItemFragment(new BasePagerAdapter.getItemFragment() {
                                @Override
                                public Fragment getItem(int position) {

                                    if(position==0){
                                        StoreAllFragment storeAllFragment = new StoreAllFragment();
                                        return storeAllFragment;
                                    }else {
                                        StoreOtherFragment otherFragment = new StoreOtherFragment();
                                        Bundle bundle = new Bundle();
                                        bundle.putString("cate_id",entity.getGoods_categories().get(position).getId());
                                        otherFragment.setArguments(bundle);
                                        return otherFragment;
                                    }
                                }
                            });

                            viewPager.setAdapter(basePagerAdapter);
                            tabLayout.setupWithViewPager(viewPager);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
//                        exceptionManager.showEmpty();
                    }
                });
        
        
    }
    
    @OnClick(R.id.tv_trading_center)
    public void tradingCenter(){
        isLogin = (boolean) SPUtil.get(getActivity(),SPUtil.IS_LOGIN,false);
        if(isLogin){
            Intent intent = new Intent(getActivity(), TradingCenterActivity.class);
            startActivity(intent);
        }else {
            goLogin();
        }
       
    }
    
    @OnClick(R.id.iv_search)
    public void search(){
        isLogin = (boolean) SPUtil.get(getActivity(),SPUtil.IS_LOGIN,false);
        if(isLogin){
            Intent intent = new Intent(getActivity(), StoreSearchActivity.class);
            startActivity(intent);
        }else {
            goLogin();
        }
    }
    
    @OnClick(R.id.iv_cart)
    public void cart(){
        isLogin = (boolean) SPUtil.get(getActivity(),SPUtil.IS_LOGIN,false);
        if(isLogin){
            Intent intent = new Intent(getActivity(), CartActivity.class);
            startActivity(intent);
        }else {
            goLogin();
        }
    }

    @Override
    public void onClick(View view) {
        getData();
    }
    
    public void getJoinGoods(){
        StoreRequest.cartList(getContext())
                .compose(DoTransform.<StoreCartListEntity>applyScheduler(getActivity(),false))
                .compose(this.<StoreCartListEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreCartListEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(StoreCartListEntity entity) {
                        if(entity.getGoods()!=null){
                            int num = entity.getGoods().size();
                            tv_goodNum.setVisibility(View.VISIBLE);
                            tv_goodNum.setText(num+"");
                        }else {
                            tv_goodNum.setVisibility(View.INVISIBLE);
                        }
                    }

                    @Override
                    protected void onFail(String msg) {

                    }
                });
    }

    public void goLogin(){
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
    
    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        if(event.is("store_join_goods")){
            getJoinGoods();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        YEventBuses.unregister(this);
    }
    
}
